
package com.cari.baju.yog;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.cari.baju.yog.database.DatabaseHelper;
import com.cari.baju.yog.entity.E_Category;
import com.cari.baju.yog.utils.Utils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;

public class SearchActivity extends Activity implements OnClickListener, ConnectionCallbacks,
        OnConnectionFailedListener {

    private Spinner spCategory;
    private ArrayAdapter<E_Category> adapter;
    private List<E_Category> listCategories;

    private Button btnSearch;
    private EditText etSearch;

    // location
    private LocationClient locationClient;
    private Location location;
    private boolean locFound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        locationClient = new LocationClient(this, this, this);

        spCategory = (Spinner) findViewById(R.id.spinner_category);
        btnSearch = (Button) findViewById(R.id.btn_cari);
        etSearch = (EditText) findViewById(R.id.et_search);

        listCategories = new ArrayList<E_Category>();
        listCategories.add(new E_Category(0, "Semua Kategori"));

        // ambil data kategori dari database
        List<E_Category> tmp = DatabaseHelper.getInstance(this).getAllCategory();
        listCategories.addAll(tmp);

        adapter = new ArrayAdapter<E_Category>(this, android.R.layout.simple_dropdown_item_1line,
                listCategories);

        // set data kategori dari database ke dalam spinner
        spCategory.setAdapter(adapter);

        btnSearch.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public void onClick(View v) {

        // proses pencarian
        if (v == btnSearch) {
            // memastikan bahwa device dapat mendeteksi position / GPS
            if (locFound) {
                Bundle data = new Bundle();
                data.putString("category", adapter.getItem(spCategory.getSelectedItemPosition())
                        .getName());
                data.putInt("categoryID", adapter.getItem(spCategory.getSelectedItemPosition())
                        .getId());

                // mengambil kata kunci yang akan dicari
                data.putString("keyword", "" + etSearch.getText().toString());
                data.putDouble("lat", location.getLatitude());
                data.putDouble("lng", location.getLongitude());

                // pindah ke halaman hasil pencarian
                startActivity(new Intent(this, SearchResultActivity.class).putExtras(data));
            } else {
                // jika tidak terkoneksi GPS
                Toast.makeText(this, "Can't find ur location, check GPS !", Toast.LENGTH_SHORT)
                        .show();
            }

        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        locationClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationClient.disconnect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnected(Bundle connectionHint) {
        location = locationClient.getLastLocation();
        if (location != null) {
            locFound = true;
            Utils.TRACE("SearchActivity", "onConnected : " + location.getLatitude());
        }

    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

}
